package com.tzy.shopcascoupon.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzy.common.entity.ShopCasCoupon;
import com.tzy.shopcascoupon.mapper.ShopCasCouponMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商家代金券 服务实现类
 * </p>
 *
 * @author 刘同学
 * @since 2022-03-04
 */
@Service
public class ShopCasCouponService extends ServiceImpl<ShopCasCouponMapper, ShopCasCoupon> implements IShopCasCouponService {
    @Resource
    private ShopCasCouponMapper casCouponMapper;

    /**
     * 添加数据
     * @param entity 实体类对象
     * @return 返回受影响行数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)//开启事务
    public boolean save(ShopCasCoupon entity) {
        int jg = casCouponMapper.insert(entity);
        if (jg>0){
            return true;
        }
        return false;
    }

    /**
     * 删除数据
     * @param id 代金券id
     * @param shopId 商家id
     * @return 返回受影响行数
     */
    @Transactional(rollbackFor = Exception.class)//开启事务
    public int casCouponDel(Integer id, Integer shopId) {
        return casCouponMapper.casCouponDel(id,shopId);
    }

    /**
     * 修改数据
     * @param shopCasCoupon 实体类对象
     * @return 返回受影响行数
     */
    @Transactional(rollbackFor = Exception.class)//开启事务
    public int update(ShopCasCoupon shopCasCoupon) {
        int jg = casCouponMapper.updateById(shopCasCoupon);
        if (jg>0){
            return jg;
        }
        return 0;
    }

    /**
     * 查询数据
     * @param shopId 商家id
     * @return 返回数据集
     */
    public List<ShopCasCoupon> select(Integer shopId){
        return casCouponMapper.casCouponSel(shopId);
    }

    /**
     * 后台资产查询分页
     * @param pageNum 起始位
     * @param size 每页总条数
     * @return
     */
    public Page<ShopCasCoupon> casCouponPage(Integer pageNum, Integer size){
        if( pageNum!=null && size!=null ){
            if(pageNum <= 0 ){
                pageNum = 0;
            }else{
                pageNum=(pageNum-1)*size;
            }
        }
        List<ShopCasCoupon> list = casCouponMapper.casCouponPage(pageNum, size);
        Page<ShopCasCoupon> page = new Page<>();
        page.setRecords(list);
        page.setTotal(casCouponMapper.jiLuShu());
        return page;
    }

    public int jiLuShu(){
        int jg = casCouponMapper.jiLuShu();
        return jg;
    }

}
